package cn.utils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Copyright © 2017 sanbo Inc. All rights reserved.
 * @Description: 简单封装的工具类
 * @Version: 1.0
 * @Create: 2017年12月27日 下午5:24:22
 * @Author: sanbo
 */
public class Log {

    public static void w(String msg) {
        System.out.println("[Warnning]  >>>> " + msg);
    }

    public static void e(String msg) {
        System.err.println("[Error] >>>> " + msg);
    }

    public static void i(String msg) {
        System.out.println("[Info] >>>> " + msg);
    }

    public static void d(String msg) {
        System.out.println("[Debug] >>>> " + msg);
    }

    public static void e(String str, Throwable e) {
        e(str);
        e(getStackTrace(e));
    }

    /**
     * 将error转换成字符串
     */

    private static String getStackTrace(Throwable e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        String result = "";
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
            result = sw.toString();
        } catch (Throwable error) {
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (Throwable e1) {
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return result;
    }
}
